package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.feign.article.IArticleClient;
import com.heima.model.channel.dtos.ChannelDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmchannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.mapper.WmchannelMapper;
import com.heima.wemedia.service.WmchannelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author rookie001
 * @creat 2023-06-07-20:16
 */
@Service
public class WmchannelServiceImpl extends ServiceImpl<WmchannelMapper, WmChannel> implements WmchannelService {
    /*查询全部频道*/
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }


    /*频道名称模糊分页查询*/
    @Override
    public ResponseResult listChannel(ChannelDto dto) {
        //数据校验
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //分页查询设置
        dto.checkParam();
        IPage page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //模糊查询频道名称
        lambdaQueryWrapper.like(WmChannel::getName, dto.getName());
        //按照创建时间倒叙查询
        lambdaQueryWrapper.orderByDesc(WmChannel::getCreatedTime);
        //封装分页
        page = page(page, lambdaQueryWrapper);
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        //返回分页
        return responseResult;
    }

    @Autowired
    private IArticleClient iArticleClient;
    @Autowired
    private WmNewsMapper wmNewsMapper;

    /* 频道更改*/
    @Override
    public ResponseResult updateCha(WmchannelDto dto) {
        if (dto == null || StringUtils.isBlank(dto.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //判断当前频道是否是禁用状态
        if (!dto.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //TODO 判断当前频道是否关联文章
        Integer integer = wmNewsMapper.selectCount(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getStatus, 9)
                .eq(WmNews::getChannelId, dto.getId()));
        if (integer>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道还被关联 无法更改");
        }
        //更改表wm_channel
        WmChannel wmChannel = new WmChannel();
        BeanUtils.copyProperties(dto, wmChannel);
        wmChannel.setCreatedTime(new Date());
        updateById(wmChannel);
        //更新ap_article
        iArticleClient.updateChannel(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //频道新增
    @Override
    public ResponseResult saveCha(WmchannelDto dto) {
        if (dto == null || StringUtils.isBlank(dto.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //保存到wm_channel
        WmChannel wmChannel = new WmChannel();
        BeanUtils.copyProperties(dto, wmChannel);
        wmChannel.setCreatedTime(new Date());
        save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    //删除频道
    @Override
    public ResponseResult delCha(WmchannelDto dto) {
       //数据校验
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        Integer integer = wmNewsMapper.selectCount(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getStatus, 9)
                .eq(WmNews::getChannelId, dto.getId()));
        if (integer>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道还被关联 无法更改");
        }
        remove(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getId,dto.getId()));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
